package sys_ctl

import (
	"net/http"
	"ubt-rbac-service/internal/app/model"
	"ubt-rbac-service/internal/app/model/sys_mod"
	"ubt-rbac-service/internal/app/service/sys_serv"
	"ubt-rbac-service/internal/app/util"
	"ubt-rbac-service/internal/core/convert"
	"ubt-rbac-service/internal/core/parse"
)

type sysType struct{}

var SysTypeCtl sysType

// Save 保存分类
func (c *sysType) Save(w http.ResponseWriter, r *http.Request) {
	// 将请求的参数转换为请求结构体
	vo, errStr := parse.Parse(r, &sys_mod.SysTypeSaveReq{})
	if vo == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: errStr})
		return
	}
	req := vo.(*sys_mod.SysTypeSaveReq)
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	// 过滤关键参数值中的非法字符
	req.TypeId = util.InputFilter(req.TypeId, true)
	req.TypeName = util.InputFilter(req.TypeName, true)
	req.TypeCode = util.InputFilter(req.TypeCode, true)
	req.ShortName = util.InputFilter(req.ShortName, true)
	req.FullName = util.InputFilter(req.FullName, true)
	req.LogoUrl = util.InputFilter(req.LogoUrl, true)
	req.LogoId = util.InputFilter(req.LogoId, true)
	req.LogoName = util.InputFilter(req.LogoName, true)
	req.ParentId = util.InputFilter(req.ParentId, true)
	if req.TypeId == req.ParentId {
		util.ToJson(w, &model.ResJson{Code: 403, Msg: "对不起，不可选择当前分类为父级分类，请重试！"})
		return
	}
	// 判断编码是否存在
	extVo := &sys_mod.SysTypeExtVo{TypeId: req.TypeId, TypeName: req.TypeName, TypeCode: req.TypeCode, Kind: req.Kind, OptOrg: userInfo.DeptId}
	if ext := sys_serv.SysTypeServ.Exists(extVo); ext {
		util.ToJson(w, &model.ResJson{Code: 402, Msg: "对不起，填写的编码已存在，请更换！"})
		return
	}
	quanPin, jianPin := util.GetPinyin(req.TypeName)
	req.QuanPin = quanPin
	req.JianPin = jianPin
	req.OptOrg = userInfo.DeptId
	var (
		result *model.Result
		kind   uint8
		code   string
	)
	if req.TypeId == "" {
		// 新增
		result = sys_serv.SysTypeServ.Add(req)
		kind = 1
	} else {
		// 修改
		result = sys_serv.SysTypeServ.Update(req)
		kind = 3
	}
	if result != nil && result.RetCode == 1001 {
		switch req.Kind {
		case 1:
			code = "10010104"
		case 2:
			code = "10010201"
		case 3:
			code = "10020101"
		case 4:
			code = "10020201"
		}
		logReq := &sys_mod.SysLogSaveReq{UserId: userInfo.UserId, ModCode: code, Kind: kind, OptOrg: userInfo.DeptId}
		sys_serv.SysLogServ.Add(logReq)
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok"})
	} else {
		util.ToJson(w, &model.ResJson{Code: 500, Msg: result.RetMsg})
	}
}

// Deletes 删除分类
func (c *sysType) Deletes(w http.ResponseWriter, r *http.Request) {
	// 将请求的参数转换为请求结构体
	vo, errStr := parse.Parse(r, &sys_mod.SysTypeProReq{})
	if vo == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: errStr})
		return
	}
	req := vo.(*sys_mod.SysTypeProReq)
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	// 过滤关键参数值中的非法字符
	req.TypeIds = util.InputFilter(req.TypeIds, true)
	req.OptOrg = userInfo.DeptId
	result := sys_serv.SysTypeServ.Deletes(req)
	if result != nil && result.RetCode == 1001 {
		var code string
		switch req.Kind {
		case 1:
			code = "10010104"
		case 2:
			code = "10010201"
		case 3:
			code = "10020101"
		case 4:
			code = "10020201"
		}
		logReq := &sys_mod.SysLogSaveReq{UserId: userInfo.UserId, ModCode: code, Kind: 2, OptOrg: userInfo.DeptId}
		sys_serv.SysLogServ.Add(logReq)
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok"})
	} else {
		util.ToJson(w, &model.ResJson{Code: 500, Msg: result.RetMsg})
	}
}

// One 获取分类详情
func (c *sysType) One(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	// 过滤关键参数值中的非法字符
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	typeId := u.Get("typeId")
	typeId = util.InputFilter(typeId, true)
	if typeId == "" || len(typeId) != 21 {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的编号参数为空或不正确，请重试！"})
		return
	}
	result := sys_serv.SysTypeServ.One(typeId)
	if result != nil && result.RetCode == 1001 {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: result.RetData})
	} else {
		util.ToJson(w, &model.ResJson{Code: 404, Msg: result.RetMsg})
	}
}

// List 获取分类树状列表
func (c *sysType) List(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	kinds := u.Get("kind")
	kinds = util.InputFilter(kinds, true)
	kind := convert.Str2u8(kinds)
	if kind == 0 {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的种类参数为空或不正确，请重试！"})
		return
	}
	req := &sys_mod.SysTypeListReq{Kind: kind, OptOrg: userInfo.DeptId}
	if result := sys_serv.SysTypeServ.List(req); result != nil && result.RetCode == 1001 {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: result.RetData})
	} else {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "暂无内容"})
	}
}

// Tree 获取分类树状列表
func (c *sysType) Tree(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	kinds := u.Get("kind")
	kinds = util.InputFilter(kinds, true)
	kind := convert.Str2u8(kinds)
	if kind == 0 {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的种类参数为空或不正确，请重试！"})
		return
	}
	req := &sys_mod.SysTypeListReq{Kind: kind, OptOrg: userInfo.DeptId}
	if result := sys_serv.SysTypeServ.Tree(req); result != nil && result.RetCode == 1001 {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: result.RetData})
	} else {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "暂无内容"})
	}
}

// Sort 获取分类数量，用于排序
func (c *sysType) Sort(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	kinds := u.Get("kind")
	parentId := u.Get("parentId")
	kinds = util.InputFilter(kinds, true)
	parentId = util.InputFilter(parentId, true)
	kind := convert.Str2u8(kinds)
	if kind == 0 || parentId == "" || len(parentId) != 21 {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数为空或不正确，请重试！"})
		return
	}
	req := &sys_mod.SysTypeSortReq{ParentId: parentId, Kind: kind, OptOrg: userInfo.DeptId}
	result := sys_serv.SysTypeServ.Sort(req)
	util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: result.RetData})
}

// Select 下拉选择单级分类
func (c *sysType) Select(w http.ResponseWriter, r *http.Request) {
	// 通过头部请求的token获取用户信息
	token := r.Header.Get("token")
	userInfo := util.GetUserInfo(token)
	if userInfo == nil {
		util.ToJson(w, &model.ResJson{Code: 401, Msg: "对不起，您未登录或登录超时，请登录！"})
		return
	}
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	kinds := u.Get("kind")
	parentId := u.Get("parentId")
	kinds = util.InputFilter(kinds, true)
	parentId = util.InputFilter(parentId, true)
	kind := convert.Str2u8(kinds)
	if kind == 0 || parentId == "" || len(parentId) != 21 {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数为空或不正确，请重试！"})
		return
	}
	req := &sys_mod.SysTypeSelReq{Kind: kind, OptOrg: userInfo.DeptId, ParentId: parentId}
	result := sys_serv.SysTypeServ.Select(req)
	util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: result.RetData})
}
